package com.example.kindergarten.util

import cn.hutool.core.date.DateUtil
import com.example.kindergarten.exceptionHandler.BaseErrorInfoInterface
import com.example.kindergarten.exceptionHandler.ExceptionEnum
import java.util.*

/**
 * @description: 自定义数据传输
 * @author: DT
 * @date: 2021/4/19 21:47
 * @version: v1.0
 */
class ResultResponse {
    /**
     * 响应代码
     */
    var code: String? = null

    /**
     * 响应消息
     */
    var message: String? = null

    /**
     * 响应结果
     */
    var result: Any? = null

    /**
     * 响应时间
     */
    var time: String = "${DateUtil.date(System.currentTimeMillis())}"

    constructor() {}
    constructor(errorInfo: BaseErrorInfoInterface) {
        this.code = errorInfo.resultCode
        message = errorInfo.resultMsg
    }

    override fun toString(): String {
        return "$this"
    }

    companion object {
        /**
         * 成功
         *
         * @return
         */
        @JvmOverloads
        fun success(data: Any? = null): ResultResponse {
            val rb = ResultResponse()
            rb.code = ExceptionEnum.SUCCESS.resultCode
            rb.message = ExceptionEnum.SUCCESS.resultMsg
            rb.result = data
            return rb
        }

        /**
         * 失败
         */
        fun error(errorInfo: BaseErrorInfoInterface): ResultResponse {
            val rb = ResultResponse()
            rb.code = errorInfo.resultCode
            rb.message = errorInfo.resultMsg
            rb.result = null
            return rb
        }

        /**
         * 失败
         */
        fun error(code: String?, message: String?): ResultResponse {
            val rb = ResultResponse()
            rb.code = code
            rb.message = message
            rb.result = null
            return rb
        }

        /**
         * 失败
         */
        fun error(message: String?): ResultResponse {
            val rb = ResultResponse()
            rb.code = "-1"
            rb.message = message
            rb.result = null
            return rb
        }
    }
}
